package com.maas.util.mybatis.jpa.littlenb.mapper;


import com.littlenb.mybatisjpa.annotation.InsertDefinition;
import com.littlenb.mybatisjpa.annotation.UpdateDefinition;
import com.littlenb.mybatisjpa.support.Certainty;
import com.littlenb.mybatisjpa.type.SelectorStrategy;
import com.maas.util.mybatis.jpa.littlenb.annotation.mapper.InsertExDefinition;
import com.maas.util.mybatis.jpa.littlenb.annotation.mapper.SelectDefinition;
import com.maas.util.mybatis.jpa.littlenb.conditions.kot.common.CT;
import com.maas.util.mybatis.jpa.littlenb.conditions.kot.provider.BaseProvider;
import com.maas.util.mybatis.jpa.littlenb.conditions.plus.core.metadata.IPage;
import com.maas.util.mybatis.jpa.littlenb.conditions.plus.core.query.QueryWrapper;
import com.maas.util.mybatis.jpa.littlenb.conditions.plus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
//import org.springframework.jdbc.core.SqlProvider;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 使用例子
 * @author sway.li
 **/
public interface JpaDemoMapper<T> {
  /**
   * insertxxx 暂时支持long 类型的 id
   * TODO 编写 InsertPlusDefinition 支持更加复杂的插入语句
   * @param t
   * @return
   */
  @InsertDefinition
  int insertJbaBatch(List<T> t);

  @InsertDefinition
  int insertJbaObj(T t);

  @InsertDefinition(strategy = SelectorStrategy.IGNORE_NULL)
  int insertIgnoreNull(T t);

  @InsertDefinition(strategy = SelectorStrategy.CERTAIN)
  int insertCertain(Certainty<T> certainty);

  @UpdateDefinition
  int updateJbaById(T t);

  /**
   * 忽略空
   * @param t
   * @return
   */
  @UpdateDefinition(strategy = SelectorStrategy.IGNORE_NULL)
  int updateByIdIgnoreNull(T t);

  /**
   * 不允空
   * @param certainty
   * @return
   */
  @UpdateDefinition(strategy = SelectorStrategy.CERTAIN, where = " id = #{entity.id}")
  int updateByIdCertain(Certainty<T> certainty);

  /**
   * 查询功能
   * 这里的作用是演示
   * TODO  domainClass = Map.class, 现阶段只能返回 @Entity 的实体类
   * @param certainty
   * @return
   */
  @SelectDefinition(strategy = SelectorStrategy.CERTAIN, where = " id = #{entity.id}", domainClass = User.class)
  int selectByIdCertain(Certainty<T> certainty);


  /**
   * 支持selectKey 序列功能，目前是直接从 xml 的  insert 获取
   * @param t
   * @return
   */
  @InsertExDefinition()
  int insertJbaExObj(T t);


//  @SelectProvider(type = SqlProvider.class, method = "selectUserCheck")
//  @ResultMap("userMap")
//  public User getUserCheck(@Param("userId") long userId, String password);

  /**
   * 演示   @SelectProvider 用法
   * @param columns
   * @param conditionList
   * @param conditionMap
   * @param entity
   * @return
   */
  @SelectProvider(type = BaseProvider.class,method="list")
  List<T> list(@Param(CT.COLUMNS) Set<String> columns, @Param(CT.SQL_CONDITION) String conditionList, @Param(CT.ALIAS_CONDITION) Map<String, Object> conditionMap, @Param(CT.ALIAS_ENTITY) T entity);


  /**
   * 演示   @Select用法
   * @param wrapper
   * @return
   */
  @Select("SELECT\n" +
          "\t ${ew.sqlSelect} \n" +
          "FROM\n" +
          "\t rc_customer_staff staff \n" +
          "LEFT JOIN rc_staff_group_ref ref ON ref.staff_id = staff.id\n" +
          "${ew.customSqlSegment}" +
          " GROUP BY  staff.id")
  IPage<Object> selectList(@Param(Constants.WRAPPER) QueryWrapper<T> wrapper);

}
